'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')

function resolve (dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || 'vue Admin Template' // 项目名称 在settings.js中

// 如果你的端口设置为80
// 使用管理员权限执行命令行
// For example, Mac: sudo npm run
// 可以通过以下方法更改端口: methods:
// 端口= 9528 npm run dev或npm run dev --port = 9528
// development => 开发环境
// production => 生产环境
const port = process.env.port || process.env.npm_config_port || 9528 // dev port

// 所有配置项说明均可在 https://cli.vuejs.org/config/
module.exports = {
  // 如果您计划将站点部署在子路径下，则需要设置publicPath。
  // 例如GitHub Pages。如果您计划将站点部署到https://foo.github.io/bar/,
  //  那么publicPath应该被设置为"/bar/".
  //  在大多数情况下请使用 '/' !!!
  //Detail:https://cli.vuejs.org/config/#publicpath
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  // lintOnSave: process.env.NODE_ENV === 'development',
  lintOnSave: false,
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    // mock-server会导致代理服务的异常
    // before: require('./mock/mock-server.js')

    // 代理跨域的配置
    proxy: {
      // 当我们的本地的请求 有/api的时候 就会代理我们的请求地址向另一个服务器发出请求
      // '/api' 不是固定的 可以随便设置 例如 /abc，/def都可以 也可以设置多个api
      // /api 要和开发环境里的基础地址"VUE_APP_BASE_API = '/api'"保持一致
      '/api': {
        target: 'http://ihrm.itheima.net/', // 跨域请求的地址
        // 另外一个服务器地址：http://ihrm.itheima.net/  此服务器管理员密码禁止修改
        // http://ihrm-java.itheima.net/
        changeOrigin: true //是否跨域 需要设置为true，才可以让本地服务代理我们发出请求
        // 路径重写
        // pathRewrites: {
        //   // 重新路由 localhost:8888/api/login => www.baidu.com/api/login
        //   '^api':''
        // }
      }
    }
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  chainWebpack (config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
